﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using 聚欣订单分析.Ling;
using System.Text;

namespace 聚欣订单分析
{
    public partial class PwdUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.Title = " 订单分析系统";
            }
            if (Session["qx"] == null)
            {
                Response.Redirect("login.aspx");
            }
        }

        bool IsNumeric(string str) //接收一个string类型的参数,保存到str里
        {
            if (str == null || str.Length == 0)    //验证这个参数是否为空
                return false;                           //是，就返回False
            ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例
            byte[] bytestr = ascii.GetBytes(str);         //把string类型的参数保存到数组里

            foreach (byte c in bytestr)                   //遍历这个数组里的内容
            {
                if (c < 48 || c > 57)                          //判断是否为数字
                {
                    return false;                              //不是，就返回False
                }
            }
            return true;                                        //是，就返回True
        }

        protected void btnUpdataPwd_Click(object sender, EventArgs e)
        {
            string oldPwd = txtOld.Text.Trim();
            string newPwd = txtNewPwd.Text.Trim();
            string qrPwd = txtQrPwd.Text.Trim();
            if (oldPwd == "" || newPwd == "" || qrPwd == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('请填写完整!')</script>");
                return;
            }
            if (newPwd.Length < 6)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('新密码的长度不能小于6位!')</script>");
                return;
            }
            if (IsNumeric(newPwd))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('新密码必须包含数字和字母!')</script>");
                return;
            }
            int i = 0;
            using (tbl_userDataContext userData = new tbl_userDataContext())
            {
                i = userData.tbl_user.Where(p => p.ID.ToString() == Session["UserID"].ToString() && p.pwd == oldPwd).ToList().Count;
            }
            if (i == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('旧密码不正确!')</script>");
                return;
            }
            if (newPwd != qrPwd)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('2次输入的密码不一致!')</script>");
                return;
            }
            else
            {
                using (tbl_userDataContext userData = new tbl_userDataContext())
                {
                    var d = userData.tbl_user.Where(p => p.ID.ToString() == Session["UserID"].ToString()).SingleOrDefault();
                    d.pwd = newPwd;
                    userData.SubmitChanges();
                }
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('修改成功!')</script>");
            }
        }
    }
}
